package com.example.myapplication;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class RadioButtonActivity extends AppCompatActivity {
    private RadioGroup gender;
    private TextView gender_text;
    private TextView hobby;
    private Button sumbit;
    private CheckBox checkBox1, checkBox2, checkBox3,checkBox4;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_radio_button);
        gender = findViewById(R.id.radioGroup);
        gender_text = findViewById(R.id.gender);
        gender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = findViewById(checkedId);
                gender_text.setText(radioButton.getText());
            }
        });
        hobby = findViewById(R.id.hobby);
        sumbit = findViewById(R.id.submit);
        checkBox1 = findViewById(R.id.yuanshen);
        checkBox2 = findViewById(R.id.mingchao);
        checkBox3 = findViewById(R.id.juequling);
        checkBox4 = findViewById(R.id.wangzherongyao);
        sumbit.setOnClickListener(v -> {
            String text = "";
            if (checkBox1.isChecked()) {
                text += checkBox1.getText() + " ";
            }
            if (checkBox2.isChecked()) {
                text += checkBox2.getText() + " ";
            }
            if (checkBox3.isChecked()) {
                text += checkBox3.getText() + " ";
            }
            if (checkBox4.isChecked()) {
                text += checkBox4.getText() + " ";
            }
            if (text.isEmpty()){
                Toast.makeText(this, "至少选择一个兴趣", Toast.LENGTH_SHORT).show();
            }else {
                hobby.setText(text);
            }
        });


        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }
}